package com.hd.sup.config;

import com.hd.common.constant.SysConstant;
import com.hd.common.xss.XssFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title XssFilterConfig.java
 * @Description XssFilter配置
 * @Package com.hd.sup.config
 * @date 2020/12/24 10:13 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Configuration
public class XssFilterConfig {

    /**
     * xss过滤拦截器
     */
    @Bean
    public FilterRegistrationBean<Filter> xssFilterRegistrationBean() {
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new XssFilter());
        filterRegistrationBean.setOrder(Integer.MAX_VALUE - 1);
        filterRegistrationBean.setEnabled(true);
        filterRegistrationBean.addUrlPatterns("/*");
        Map<String, String> initParameters = new HashMap<>();
        // excludes用于配置不需要参数过滤的请求url
        initParameters.put("excludes", "/favicon.ico,/img/*,/js/*,/css/*");
        // isIncludeRichText主要用于设置富文本内容是否需要过滤
        initParameters.put("isIncludeRichText", "true");
        // 配置过滤URL白名单
        initParameters.put(SysConstant.XSS_NOTICE_KEY, "");
        filterRegistrationBean.setInitParameters(initParameters);
        return filterRegistrationBean;
    }
}
